package com.jiot.controller.device;

import com.github.pagehelper.PageInfo;
import com.zg.cloud.BaseResponse;
import com.zg.cloud.entity.PageInfoEntity;
import com.zg.cloud.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import req.sort.SortReq;
import req.product.AddProductReq;
import req.product.DeleteProductReq;
import req.product.DetailProductReq;
import req.product.ModifyProductReq;
import resp.product.DetailProductResp;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author hushi
 * @date 2020/10/20 14:41
 * @description
 */
@RestController
@RequestMapping("/device/product")
@Api(value = "产品模块",tags = "产品列表管理")
public class ProductController {

    @Resource
    private ProductService productService;

    @PostMapping("/select/product/all")
    @ApiOperation(value = "查询产品全部", notes = "device")
    public BaseResponse selectProductAll(@RequestBody PageInfoEntity pageInfoEntity) {
        BaseResponse baseResponse = new BaseResponse();
        PageInfo pageInfo = productService.selectProductAllByPage(pageInfoEntity);
        baseResponse.setData(pageInfo);
        return baseResponse;
    }

    @PostMapping("/detail/product")
    @ApiOperation(value = "产品详情", notes = "device")
    public BaseResponse detailProduct(@RequestBody @Valid DetailProductReq detailProductReq) {
        BaseResponse baseResponse = new BaseResponse();
        DetailProductResp selectProductAllResp = productService.detailProduct(detailProductReq);
        baseResponse.setData(selectProductAllResp);
        return baseResponse;
    }

    @PostMapping(value = "/add/product")
    @ApiOperation(value = "添加产品", notes = "device")
    public BaseResponse addProduct(AddProductReq addProductReq,MultipartFile productfile) {
        productService.addProduct(addProductReq,productfile);
        return new BaseResponse();
    }

    @PostMapping("/delete/product")
    @ApiOperation(value = "删除产品", notes = "device")
    public BaseResponse deleteProduct(@RequestBody @Valid DeleteProductReq deleteProductReq) {
        productService.deleteProduct(deleteProductReq);
        return new BaseResponse();
    }

    @PostMapping("/modify/product")
    @ApiOperation(value = "修改产品", notes = "device")
    public BaseResponse modifyProduct(@RequestBody @Valid ModifyProductReq modifyProductReq) {
        productService.modifyProduct(modifyProductReq);
        return new BaseResponse();
    }

    @PostMapping("/modify/sort/product")
    @ApiOperation(value = "产品排序  0-升序，1-降序", notes = "device")
    public BaseResponse sortProduct(@RequestBody SortReq sortReq) {
        Boolean aBoolean = productService.sortProduct(sortReq.getId(), Integer.valueOf(sortReq.getSortFlag()));
        return new BaseResponse();
    }
}
